package com.jboost.modules.app.service.strategry.xmallure;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.google.gson.JsonObject;
import com.jboost.common.util.HttpRestUtils;
import com.jboost.common.util.JSONUtil;
import com.jboost.modules.app.constant.*;
import com.jboost.modules.app.dto.*;
import com.jboost.modules.system.entity.AJdySyncData;
import com.jboost.modules.system.entity.DpBanDan;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static org.jeecgframework.codegenerate.a.a.e;
import static org.jeecgframework.codegenerate.a.a.m;

//@Service(value = "XmAllureOrderBackQuery")
@Slf4j
public class XmAllureOrderBackQuery extends AbstractXmAllureUser<JSONObject, Boolean> {
    public BaseResult<Boolean> invokeStrategy(JSONObject jsonObject) throws Exception {
        XmCallBack data = jsonObject.getObject("data", XmCallBack.class);
        Map map = new HashMap();
        String header = getHeader("opportunity");
        map.put("opportunity_id", data.getXmsjid());


        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        log.info("开始批量查询Hs材料数据");

        JdyReq jdyReq = new JdyReq();
        jdyReq.setApp_id("654318875348639588ec98ac");
        jdyReq.setEntry_id("646adb8c16da3c00080da6b6");
        jdyReq.setLimit(100);
        JdyReq.FilterBean filterBean = new JdyReq.FilterBean();
        filterBean.setRel("and");
        List<JdyReq.FilterBean.CondBean> cond = new ArrayList<>();
        JdyReq.FilterBean.CondBean a = new JdyReq.FilterBean.CondBean();
        a.setField("order_no");
        a.setType("text");
        a.setMethod("eq");
        a.setValue(Arrays.asList(data.get_widget_1725006799596()));
        cond.add(a);
        filterBean.setCond(cond);
        jdyReq.setFilter(filterBean);
        Thread.sleep(60000);
        String s = jdyQueryAll(jdyReq);

        JSONArray jsonArray = (JSONArray) JSONUtil.fromJson(s, HashMap.class).get("data");
        if (jsonArray.size() > 0) {
            for (Object o : jsonArray) {
                data.setOrder_state(((JSONObject) o).getString("order_state"));
            }
        }
        String msg = XmOrderBackEnum.getMsg(data.getOrder_state());
        if (StringUtils.isEmpty(msg)){
            return BaseResult.success(true);
        }
        map.put("stage", msg);
        MediaType mediaType = MediaType.parse("application/json");
        OkHttpClient cclient = new OkHttpClient();
        RequestBody body = RequestBody.create(mediaType,JSONObject.toJSONString(map));
        Request request1 = new Request.Builder()
                .url(orderSubmitUrl).addHeader("Authorization", "Bearer " + header).post(body)
                .build();
        Response execute1 = cclient.newCall(request1).execute();
        String custmorResponse = execute1.body().string();
        return BaseResult.success(true);
    }

}
